package com.xiaoze.question.component.queScenarioTacticsComonent;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xiaoze.question.domain.QuestionAnswer;
import com.xiaoze.question.domain.QuestionAnswerUser;
import com.xiaoze.question.domain.QuestionItems;
import com.xiaoze.question.domain.QuestionScenario;
import com.xiaoze.question.dto.QuestionAnswerUserInput;
import com.xiaoze.question.dto.QuestionAnswerUserOutput;
import com.xiaoze.question.dto.QuestionAnswerUsersOutput;
import com.xiaoze.question.factory.SceTacStrategy;
import com.xiaoze.question.mapper.QuestionAnswerMapper;
import com.xiaoze.question.mapper.QuestionItemsMapper;
import com.xiaoze.question.mapper.QuestionScenarioMapper;
import com.xiaoze.question.service.impl.QuestionAnswerUserServiceImpl;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 小泽
 * @create 2024-08-12  9:58
 * 记得每天敲代码哦
 */
@Component("common")
@RequiredArgsConstructor
public class CommonComponent implements SceTacStrategy<QuestionAnswerUserInput, QuestionAnswerUsersOutput> {

    private final QuestionAnswerUserServiceImpl questionAnswerUserService;
    private final QuestionItemsMapper questionItemsMapper;
    private final QuestionScenarioMapper questionScenarioMapper;
    private final QuestionAnswerMapper questionAnswerMapper;

    @Override
    public QuestionAnswerUsersOutput selectBySceTacId(QuestionAnswerUserInput vo) {

        // 定义返回结果
        QuestionAnswerUsersOutput output = new QuestionAnswerUsersOutput();

        // 1、查询问卷开启状态
        QuestionScenario questionScenario = questionScenarioMapper.selectById(vo.getId());
        if(questionScenario != null){
            if(questionScenario.getStatus().equals("1")){ // "1" : 代表未开启
                output.setResult(Boolean.FALSE);
                return output;
            }
        }else {
            output.setResult(Boolean.FALSE);
            return output;
        }
        // 2、查看该用户是否参与过该报表（根据手机号）
        LambdaQueryWrapper<QuestionAnswerUser> queryWrapper = new LambdaQueryWrapper<QuestionAnswerUser>().eq(QuestionAnswerUser::getCategoryId, vo.getId())
                .eq(QuestionAnswerUser::getNumber, vo.getPhoneNumber());
        // 查询出的是个list，避免是该问卷由其他类型转换过来，导致数据库多条数据，会报错。
        List<QuestionAnswerUser> questionAnswerUserList = questionAnswerUserService.getBaseMapper().selectList(queryWrapper);
        // 3、不为空，证明参与过，不满与参与条件
        if(CollectionUtils.isNotEmpty(questionAnswerUserList)){
            output.setResult(Boolean.FALSE);
            return output;
        }

        // 4、满足参与条件，封装返回数据
        output.setResult(Boolean.TRUE);
        // 5、设置 问卷 信息
        BeanUtils.copyProperties(questionScenario, output);
        // 6、设置题目和答案信息
        // 6.1 查询出题目
        List<QuestionItems> questionItems = questionItemsMapper.selectList(new LambdaQueryWrapper<QuestionItems>().eq(QuestionItems::getCategoryId, vo.getId()));
        if(CollectionUtils.isNotEmpty(questionItems)){

            // 6.2 查询出题目对应的答案选项
            List<QuestionAnswerUserOutput> questionAnswerUserOutputs = new ArrayList<>();
            for (QuestionItems questionItem : questionItems) {
                QuestionAnswerUserOutput questionAnswerUserOutput = new QuestionAnswerUserOutput();
                Long questionId = questionItem.getId();
                BeanUtils.copyProperties(questionItem, questionAnswerUserOutput);
                // 处理单选或者多选（只有这两种情况才会有问题选项）
                if(questionItem.getQuestionType().equals("2") || questionItem.getQuestionType().equals("3")){
                    List<QuestionAnswer> questionAnswers = questionAnswerMapper.selectList(new LambdaQueryWrapper<QuestionAnswer>().eq(QuestionAnswer::getQuestionId, questionId));
                    questionAnswerUserOutput.setQuestionAnswerList(questionAnswers);
                }
                questionAnswerUserOutputs.add(questionAnswerUserOutput);
            }
            // 设置返回值
            output.setQuestionAnswerUserOutputs(questionAnswerUserOutputs);

        }
        return output;
    }
}
